
package boun.swe.bob.service;

import boun.swe.bob.entities.*;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.sun.tools.javac.util.Convert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: alper yucer
 * Date: Dec 26, 2010
 * Time: 11:27:51 PM
 * To change this template use File | Settings | File Templates.
 */
public class ApproveUserController  extends BaseController  {

    @RequestMapping(method = RequestMethod.POST)
       public ModelAndView handlePost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String result = httpServletRequest.getParameter("result");
        String setAdmin = httpServletRequest.getParameter("setAdmin");
        if(setAdmin == null) setAdmin = " ";
        int requestedUserId = Integer.parseInt(httpServletRequest.getParameter("requestedUserId"));


            if(result.equals("accept")) {
                User user = userDao.findByPrimaryKey(requestedUserId);
                user.setEnabled(1);


                if(setAdmin.equals(httpServletRequest.getParameter("requestedUserId")) ){

                    Authority authority = authorityDao.findByPrimaryKey(requestedUserId);
                    authority.setAuthority("ROLE_ADMIN");
                    authorityDao.update(authority);
                    mailSender.send(user.getUsername(), "Welcome To BOB - Approval Notice", "Your registration information is now approved by the admins, you can login to BOB now. Also your user right is set as Admin, you can help us to approve or reject waiting users.");

                }
                else{
                    mailSender.send(user.getUsername(), "Welcome To BOB - Approval Notice", "Your registration information is now approved by the admins, you can login to BOB now.");

                }

                userDao.update(user);

        }


        if(result.equals("reject")) {
                User user = userDao.findByPrimaryKey(requestedUserId);
                user.setEnabled(2);
                mailSender.send(user.getUsername(), "BOB rejection notice", "We are sorry to inform you that your registration information is rejected by the admins.");
                userDao.update(user);
        }

        Map<String, Object> myModel = Maps.newHashMap();


           List<User> searchResultList = new ArrayList<User>();


           searchResultList = userDao.findUnapprovedUsers();
           myModel.put("himself", true);
           myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
           myModel.put("resultList", searchResultList);
           return new ModelAndView("approveUsers", "model", myModel);

   



       }
       @RequestMapping(method = RequestMethod.GET)
        public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();


           List<User> searchResultList = new ArrayList<User>();


           searchResultList = userDao.findUnapprovedUsers();
           myModel.put("himself", true);
           myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
           myModel.put("resultList", searchResultList);
           return new ModelAndView("approveUsers", "model", myModel);
    }

}








